<?php
/**
 * Created by PhpStorm.
 * User: luoqinghui
 * Date: 2020/7/24
 * Time: 上午17:40
 */

namespace App\Http\Controllers\Api\Home;

use App\Http\Controllers\Controller;
use App\Modules\Home\Services\ArticleService;
use App\Modules\Home\Services\CategoryService;
use Illuminate\Http\Request;

/**
 * 文章管理
 * Class HomeController
 * @package App\Http\Controllers\Api\Home
 */
class ArticleController extends Controller
{
    /**
     * 获取用户协议
     */
    public function getAgreement(){
        $code = 'AGREEMENT';        //固定code
        $categoryData = CategoryService::getCategory($code);
        if ( empty($categoryData) ) return response()->success();
        $category_id = $categoryData->id;
        $articleData = ArticleService::getArticle($category_id);
        return response()->success($articleData);
    }

    /**
     * 获取问题
     */
    public function getProblem(){
        $code = 'PROBLEM';          //固定code
        $categoryData = CategoryService::getCategory($code);
        if ( empty($categoryData) ) return response()->success();
        $category_id = $categoryData->id;
        $articleData = ArticleService::getList($category_id);
        return response()->success($articleData);
    }

    /**
     * 获取分类下的文章
     */
    public function categoryArticle(Request $request){
        $category_id = $request->input('category_id',0);
        if ( empty($category_id) ) return response()->error("PARAM_EMPTY");
        $articleData = ArticleService::getList($category_id);
        return response()->success($articleData);
    }

    /**
     * 分类列表
     */
    public function categoryIndex(){
        $categoryData = CategoryService::getList();
        return response()->success($categoryData);
    }

    /**
     * 添加分类
     */
    public function addCategory(Request $request){
        $user_id = $this->getLoginUser()->id;
//        $user_id = 1;
        $category_id = $request->input('category_id',0);
        $code = $request->input('code','');
        $name = $request->input('name','');
        $sort = $request->input('sort',0);
        if ( empty($name) ) return response()->error("PARAM_EMPTY");
        //添加数据
        $categoryAddArr = [
            'user_id'       => $user_id,
            'category_id'   => $category_id,
            'code'          => $code,
            'name'          => $name,
            'sort'          => $sort
        ];
        $result = CategoryService::addCategory($categoryAddArr);
        if ( empty($result) ) return response()->error("ADD_FAILURE");
        return response()->success();
    }

    /**
     * 修改分类
     */
    public function updateCategory(Request $request){
        $id = $request->input('id',0);
        if ( empty($id) ) return response()->error("PARAM_EMPTY");
        $cateUpdateArr = ['id'=>$id];
        !isset($_POST['category_id']) ? : $cateUpdateArr['category_id'] = $request->input('category_id',0);
        !isset($_POST['code']) ? : $cateUpdateArr['code'] = $request->input('code','');
        !isset($_POST['name']) || empty($_POST['name']) ? : $cateUpdateArr['name'] = $request->input('name','');
        !isset($_POST['sort']) ? : $cateUpdateArr['sort'] = $request->input('sort',0);
        $result = CategoryService::updateCategory($cateUpdateArr);
        if ( empty($result) && $result !== 0 ) return response()->error("UPDATE_FAILURE");
        return response()->success();
    }

    /**
     * 删除分类
     */
    public function deleteCategory(Request $request){
        $id = $request->input('id',0);
        if ( empty($id) ) return response()->error("PARAM_EMPTY");
        $cateUpdateArr = ['id'=>$id,'status'=>-1];
        $result = CategoryService::updateCategory($cateUpdateArr);
        if ( empty($result) && $result !== 0 ) return response()->error("UPDATE_FAILURE");
        return response()->success();
    }

    /**
     * 添加文章
     */
    public function addArticle(Request $request){
        $user_id = $this->getLoginUser()->id;
//        $user_id = 1;
        $category_id = $request->input('category_id',0);
        $title = $request->input('title','');
        $content = $request->input('content','');
        $sort = $request->input('sort',0);
        if ( empty($content) ) return response()->error("PARAM_EMPTY");
        //添加数据
        $categoryAddArr = [
            'user_id'       => $user_id,
            'category_id'   => $category_id,
            'title'          => $title,
            'content'          => $content,
            'sort'          => $sort
        ];
        $result = ArticleService::addArticle($categoryAddArr);
        if ( empty($result) ) return response()->error("ADD_FAILURE");
        return response()->success();
    }

    /**
     * 修改文章
     */
    public function updateArticle(Request $request){
        $id = $request->input('id',0);
        if ( empty($id) ) return response()->error("PARAM_EMPTY");
        $updateArr = ['id'=>$id];
        !isset($_POST['category_id']) ? : $updateArr['category_id'] = $request->input('category_id',0);
        !isset($_POST['title']) ? : $updateArr['title'] = $request->input('title','');
        !isset($_POST['content']) || empty($_POST['content']) ? : $updateArr['content'] = $request->input('content','');
        !isset($_POST['sort']) ? : $updateArr['sort'] = $request->input('sort',0);
        $result = ArticleService::updateArticle($updateArr);
        if ( empty($result) && $result !== 0 ) return response()->error("UPDATE_FAILURE");
        return response()->success();
    }

    /**
     * 删除文章
     */
    public function deleteArticle(Request $request){
        $id = $request->input('id',0);
        if ( empty($id) ) return response()->error("PARAM_EMPTY");
        $updateArr = ['id'=>$id,'status'=>-1];
        $result = ArticleService::updateArticle($updateArr);
        if ( empty($result) && $result !== 0 ) return response()->error("UPDATE_FAILURE");
        return response()->success();
    }

}
























